package net.yihang.service;

import com.google.common.cache.Cache;
import net.yihang.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    private final BeanPropertyRowMapper<Product> rowMapper = new BeanPropertyRowMapper<>(Product.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private Cache<String,Object> cache;

    public static final String FIND_ALL = "select * from demo_product";
    public static final String FIND_BY_ID = "select * from demo_product where id = ?";

    @net.yihang.aspect.Cacheable(keyPrefix="ProductService.findById", argumentNames = {"id"})
    public Product findById(int id) {
        return jdbcTemplate.queryForObject(FIND_BY_ID, rowMapper, id);
    }

    @net.yihang.aspect.Cacheable(keyPrefix="ProductService.findAll")
    public List<Product> findAll() {
        return jdbcTemplate.query(FIND_ALL, rowMapper);
    }
}
